/*
 * TestFrame.java
 *
 * Created on 17 marzo 2007, 18.38
 */

package com.bloomingstars.bluepad.desktop.client;

import com.bloomingstars.bluepad.comm.CommType;
import com.bloomingstars.bluepad.comm.CommandCode;
import com.bloomingstars.bluepad.comm.ConnectionStatus;
import com.bloomingstars.bluepad.comm.DA2Address;
import com.bloomingstars.bluepad.comm.DA2Comm;
import com.bloomingstars.bluepad.comm.DA2CommFactory;
import com.bloomingstars.bluepad.comm.DA2CommListener;
import com.bloomingstars.bluepad.comm.SetupStatus;
import com.bloomingstars.bluepad.comm.TxStatus;
import javax.swing.JOptionPane;

/**
 *
 * @author  vega
 */
public class TestFrame extends javax.swing.JFrame implements DA2CommListener
{
    
    /** Creates new form TestFrame */
    public TestFrame()
    {
        comm = DA2CommFactory.createDA2Comm(CommType.BLUETOOTH);
        
        comm.addCommListener(this);
        
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents()
    {
        buttonGroup1 = new javax.swing.ButtonGroup();
        btNorth = new javax.swing.JButton();
        btSouth = new javax.swing.JButton();
        btEast = new javax.swing.JButton();
        btWest = new javax.swing.JButton();
        btConnect = new javax.swing.JButton();
        btDisconnect = new javax.swing.JButton();
        btSpeed1 = new javax.swing.JRadioButton();
        btSpeed2 = new javax.swing.JRadioButton();
        btSpeed3 = new javax.swing.JRadioButton();
        btSpeed4 = new javax.swing.JRadioButton();
        btGetCoords = new javax.swing.JButton();
        tfRA = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        tfDec = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jButton8 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        btNorth.setText("N");
        btNorth.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mousePressed(java.awt.event.MouseEvent evt)
            {
                btNorthMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt)
            {
                btNorthMouseReleased(evt);
            }
        });

        btSouth.setText("S");
        btSouth.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mousePressed(java.awt.event.MouseEvent evt)
            {
                btSouthMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt)
            {
                btSouthMouseReleased(evt);
            }
        });

        btEast.setText("E");
        btEast.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mousePressed(java.awt.event.MouseEvent evt)
            {
                btEastMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt)
            {
                btEastMouseReleased(evt);
            }
        });

        btWest.setText("W");
        btWest.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mousePressed(java.awt.event.MouseEvent evt)
            {
                btWestMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt)
            {
                btWestMouseReleased(evt);
            }
        });

        btConnect.setText("Connect");
        btConnect.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btConnectActionPerformed(evt);
            }
        });

        btDisconnect.setText("Disconnect");
        btDisconnect.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btDisconnectActionPerformed(evt);
            }
        });

        buttonGroup1.add(btSpeed1);
        btSpeed1.setSelected(true);
        btSpeed1.setText("1");
        btSpeed1.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        btSpeed1.setMargin(new java.awt.Insets(0, 0, 0, 0));
        btSpeed1.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mouseClicked(java.awt.event.MouseEvent evt)
            {
                btSpeed1MouseClicked(evt);
            }
        });

        buttonGroup1.add(btSpeed2);
        btSpeed2.setText("2");
        btSpeed2.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        btSpeed2.setMargin(new java.awt.Insets(0, 0, 0, 0));
        btSpeed2.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mouseClicked(java.awt.event.MouseEvent evt)
            {
                btSpeed2MouseClicked(evt);
            }
        });

        buttonGroup1.add(btSpeed3);
        btSpeed3.setText("3");
        btSpeed3.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        btSpeed3.setMargin(new java.awt.Insets(0, 0, 0, 0));
        btSpeed3.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mouseClicked(java.awt.event.MouseEvent evt)
            {
                btSpeed3MouseClicked(evt);
            }
        });

        buttonGroup1.add(btSpeed4);
        btSpeed4.setText("4");
        btSpeed4.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        btSpeed4.setMargin(new java.awt.Insets(0, 0, 0, 0));
        btSpeed4.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mouseClicked(java.awt.event.MouseEvent evt)
            {
                btSpeed4MouseClicked(evt);
            }
        });

        btGetCoords.setText("Get Coords");
        btGetCoords.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btGetCoordsActionPerformed(evt);
            }
        });

        jLabel1.setText("RA");

        jLabel2.setText("DEC");

        jButton8.setText("Quit");
        jButton8.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton8ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(btEast)
                .add(34, 34, 34)
                .add(btWest)
                .addContainerGap(220, Short.MAX_VALUE))
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                .add(47, 47, 47)
                                .add(btSouth))
                            .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                .add(28, 28, 28)
                                .add(btSpeed1)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(btSpeed2)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(btSpeed3)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(btSpeed4)))
                        .add(94, 94, 94)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                        .add(jLabel1)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))
                                    .add(layout.createSequentialGroup()
                                        .add(jLabel2)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)))
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(tfRA, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)
                                    .add(tfDec, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)))
                            .add(btGetCoords)))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(46, 46, 46)
                        .add(btNorth)
                        .add(70, 70, 70)
                        .add(btConnect)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btDisconnect)))
                .add(27, 27, 27))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(143, Short.MAX_VALUE)
                .add(jButton8)
                .add(150, 150, 150))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(layout.createSequentialGroup()
                        .add(38, 38, 38)
                        .add(btNorth)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(btConnect)
                            .add(btDisconnect))
                        .add(21, 21, 21)))
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btEast)
                    .add(btWest))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btSouth)
                    .add(btGetCoords))
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(37, 37, 37)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(btSpeed1)
                            .add(btSpeed2)
                            .add(btSpeed3)
                            .add(btSpeed4))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 20, Short.MAX_VALUE)
                        .add(jButton8))
                    .add(layout.createSequentialGroup()
                        .add(14, 14, 14)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel1)
                            .add(tfRA, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel2)
                            .add(tfDec, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btGetCoordsActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btGetCoordsActionPerformed
    {//GEN-HEADEREND:event_btGetCoordsActionPerformed
       comm.send(CommandCode.GET_TELESCOPE_RA);
       
       try
       {
           Thread.sleep(250);
       }
       catch (Exception ex)
       {
           
       }
       
       comm.send(CommandCode.GET_TELESCOPE_DEC);
    }//GEN-LAST:event_btGetCoordsActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton8ActionPerformed
    {//GEN-HEADEREND:event_jButton8ActionPerformed
        System.exit(0);
    }//GEN-LAST:event_jButton8ActionPerformed

    private void btSpeed4MouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_btSpeed4MouseClicked
    {//GEN-HEADEREND:event_btSpeed4MouseClicked
        int st = comm.send(CommandCode.SET_SPEED_4);
        
        System.out.println(SetupStatus.getStringFromCode(st));
    }//GEN-LAST:event_btSpeed4MouseClicked

    private void btSpeed3MouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_btSpeed3MouseClicked
    {//GEN-HEADEREND:event_btSpeed3MouseClicked
        int st = comm.send(CommandCode.SET_SPEED_3);
        
        System.out.println(st);
    }//GEN-LAST:event_btSpeed3MouseClicked

    private void btSpeed2MouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_btSpeed2MouseClicked
    {//GEN-HEADEREND:event_btSpeed2MouseClicked
        int st = comm.send(CommandCode.SET_SPEED_2);
        
        System.out.println(st);
    }//GEN-LAST:event_btSpeed2MouseClicked

    private void btSpeed1MouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_btSpeed1MouseClicked
    {//GEN-HEADEREND:event_btSpeed1MouseClicked
        int st = comm.send(CommandCode.SET_SPEED_1);
        
        System.out.println(st);
    }//GEN-LAST:event_btSpeed1MouseClicked

    private void btEastMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_btEastMouseReleased
    {//GEN-HEADEREND:event_btEastMouseReleased
        int st = comm.send(CommandCode.RELEASE_EAST);
        
        System.out.println(st);
    }//GEN-LAST:event_btEastMouseReleased

    private void btEastMousePressed(java.awt.event.MouseEvent evt)//GEN-FIRST:event_btEastMousePressed
    {//GEN-HEADEREND:event_btEastMousePressed
        int st = comm.send(CommandCode.MOVE_EAST);
        
        System.out.println(st);
    }//GEN-LAST:event_btEastMousePressed

    private void btSouthMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_btSouthMouseReleased
    {//GEN-HEADEREND:event_btSouthMouseReleased
        int st = comm.send(CommandCode.RELEASE_SOUTH);
        
        System.out.println(st);
    }//GEN-LAST:event_btSouthMouseReleased

    private void btSouthMousePressed(java.awt.event.MouseEvent evt)//GEN-FIRST:event_btSouthMousePressed
    {//GEN-HEADEREND:event_btSouthMousePressed
        int st = comm.send(CommandCode.MOVE_SOUTH);
        
        System.out.println(st);
    }//GEN-LAST:event_btSouthMousePressed

    private void btWestMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_btWestMouseReleased
    {//GEN-HEADEREND:event_btWestMouseReleased
        int st = comm.send(CommandCode.RELEASE_WEST);
        
        System.out.println(st);
    }//GEN-LAST:event_btWestMouseReleased

    private void btWestMousePressed(java.awt.event.MouseEvent evt)//GEN-FIRST:event_btWestMousePressed
    {//GEN-HEADEREND:event_btWestMousePressed
        int st = comm.send(CommandCode.MOVE_WEST);
        
        System.out.println(st);
    }//GEN-LAST:event_btWestMousePressed

    private void btNorthMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_btNorthMouseReleased
    {//GEN-HEADEREND:event_btNorthMouseReleased
        int st = comm.send(CommandCode.RELEASE_NORTH);
        
        System.out.println(st);
    }//GEN-LAST:event_btNorthMouseReleased

    private void btNorthMousePressed(java.awt.event.MouseEvent evt)//GEN-FIRST:event_btNorthMousePressed
    {//GEN-HEADEREND:event_btNorthMousePressed
        int st = comm.send(CommandCode.MOVE_NORTH);
        
        System.out.println(st);
    }//GEN-LAST:event_btNorthMousePressed

    private void btDisconnectActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btDisconnectActionPerformed
    {//GEN-HEADEREND:event_btDisconnectActionPerformed
        comm.disconnect();
    }//GEN-LAST:event_btDisconnectActionPerformed

    private void btConnectActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btConnectActionPerformed
    {//GEN-HEADEREND:event_btConnectActionPerformed
        
        
        if (comm.getSetupStatus() != SetupStatus.SETUP_OK)
        {
            DA2Address address = new DA2Address("da2", DA2Address.DEVICE_NAME);
            int ss = comm.setup(address);
            
            if (ss != SetupStatus.SETUP_OK)
            {
                JOptionPane.showMessageDialog(null, 
                                              "Connection setup failed", 
                                              "Alert", 
                                              JOptionPane.ERROR_MESSAGE); 
            }
        }
        
        int cs = comm.connect();

        if (cs != ConnectionStatus.CONN_OK)
        {
            JOptionPane.showMessageDialog(null, 
                                          "Connection OK!", 
                                          "Alert", 
                                          JOptionPane.INFORMATION_MESSAGE); 

        }            
    }//GEN-LAST:event_btConnectActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new TestFrame().setVisible(true);
            }
        });
    }
    
    public void connectionLost()
    {
        JOptionPane.showMessageDialog(null, 
                                      "Connection lost", 
                                      "Alert", 
                                      JOptionPane.ERROR_MESSAGE);         
    }
    
    public void dataReceived(String data, int lastCommand)
    {
       switch (lastCommand)
       {
           case CommandCode.GET_TELESCOPE_RA:
               tfRA.setText(data);
               break;
               
           case CommandCode.GET_TELESCOPE_DEC:
               tfDec.setText(data);
               break;
       }
    }
    
    
    private DA2Comm comm;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btConnect;
    private javax.swing.JButton btDisconnect;
    private javax.swing.JButton btEast;
    private javax.swing.JButton btGetCoords;
    private javax.swing.JButton btNorth;
    private javax.swing.JButton btSouth;
    private javax.swing.JRadioButton btSpeed1;
    private javax.swing.JRadioButton btSpeed2;
    private javax.swing.JRadioButton btSpeed3;
    private javax.swing.JRadioButton btSpeed4;
    private javax.swing.JButton btWest;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton8;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField tfDec;
    private javax.swing.JTextField tfRA;
    // End of variables declaration//GEN-END:variables
    
}
